package com.tf_cut.admin.service.controller;

import com.alibaba.fastjson.JSON;
import com.tf_cut.admin.annotation.Decrypt;
import com.tf_cut.admin.annotation.Encrypt;
import com.tf_cut.admin.backend.api.BackEndApi;
import com.tf_cut.admin.common.pojo.PageInfo;
import com.tf_cut.admin.common.pojo.Result;
import com.tf_cut.admin.seller.vo.SellerRefAgentVo;
import com.tf_cut.admin.service.vo.ServiceRefSellerResVo;
import com.tf_cut.admin.service.vo.ServiceRefSellerVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.servlet.ModelAndView;


@RestController
@RequestMapping("/service/ref/")
@Slf4j
public class ServiceRefSellerController {
	
	@Autowired
	private BackEndApi serviceApi;
	
	// 进入关联清单页面
	@GetMapping("reflist")
    public ModelAndView refList() {
        return new ModelAndView("service/ref/reflist", "service", null);
    }
	
	// 进入关联页面
	@GetMapping("refinsert")
    public ModelAndView refInsert() {
        return new ModelAndView("service/ref/refinsert", "service", null);
    }

	// 进入关联更新页面
	@GetMapping("refupdate")
    public ModelAndView refupdate(String relationId,String agentName,String sellerName) {
		SellerRefAgentVo avo = null == relationId ? 
				new SellerRefAgentVo() : 
					JSON.parseObject(serviceApi.getSellerRef(relationId),SellerRefAgentVo.class);
		avo.setAgentName(agentName);
		avo.setSellerName(sellerName);
        return new ModelAndView("service/ref/refupdate", "service", avo);
    }

	/**
	 * use
	 * @param avo
	 * @return
	 */
	@SuppressWarnings("unchecked")
	@PostMapping("reflist/page")
    public Result<PageInfo<ServiceRefSellerResVo>> page(ServiceRefSellerVo avo) {
        String json = JSON.toJSONString(avo);
        json = serviceApi.findListServiceRefSeller(json);
        return Result.of(JSON.parseObject(json, PageInfo.class));
    }
	
	@SuppressWarnings("unchecked")
	@Decrypt
	@Encrypt
	@PostMapping("saveRef")
    public Result<SellerRefAgentVo> saveRef(ServiceRefSellerVo avo) {
        String json = JSON.toJSONString(avo);
        json = serviceApi.saveSellerRefService(json);
        return JSON.parseObject(json, Result.class);
    }
	
	@SuppressWarnings("unchecked")
	@PostMapping("operation")
	public Result<Boolean> operation(ServiceRefSellerVo avo) {
		return JSON.parseObject(serviceApi.operationServiceSellerRef(avo.getServiceSellerRelationId(),avo.getRefState()),Result.class);
	}
}
